package com.yqfk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqfk.auto.JSONAuthentication;
import com.yqfk.entity.YClass;
import com.yqfk.entity.YLevel;
import com.yqfk.mapper.YClassMapper;
import com.yqfk.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yqfk
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/y-class")
public class YClassController extends JSONAuthentication {
    @Autowired
    YClassMapper yClassMapper;
    @ApiOperation(value = "添加用户的班级信息")
    @PostMapping("/add")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody YClass yClass) throws Exception {
        YClass yClass1 = yClassMapper.selectById(yClass.getUid());
        if(yClass1!=null){
            Result error = Result.error().message("该学号已绑定用户");
            this.WriteJSON(request, response, error);
        }else{
            if (yClassMapper.insert(yClass) > 0) {
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }

    }
    @ApiOperation(value = "根据用户id删除绑定的班级学号信息")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        if (yClassMapper.delete(wrapper) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

